package cz.tpj.upl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cz.tpj.database.mesto.City;
import cz.tpj.database.mesto.Table_city;
import cz.tpj.logic.Navigation;
import cz.tpj.util.ErrorDatabase;

public class AddCityUtil {
	//konstanty pro komunikaci s insert JSP
	public static final String NEXT_PAGE = "attr_add_images";
	//-------konstanty pro formular---------
	public static final String CITY_ID	  = "city_id";
	public static final String CITY_NAME  = "city_name";
	public static final String SHORT_DESC = "short_desc";
	public static final String LONG_DESC  = "long_desc";
	public static final String MAP_NAME   = "map_name";
	public static final String ACTION     = "action";
	public static final String INSERT_CITY_ACTION = "insert_city_action";
	//oznaceni pro pole uploadovanych obrazku mest
	public static final String UPLOADED_CITIES = "uploadet_cities"; 
	
	//--------------------------------------
	//formularove promenne
	private String cityName;
	private String mapName;
	private String shortDesc;
	private String longDesc;
	private HttpServletRequest request;
	private String message = new String();
	
	public AddCityUtil(HttpServletRequest request){
		this.request = request;
		
	}
	
	public void saveInDatabese(){
		//nacte formularova data
		fetchParametrs();
		try {
			//podle akce bud ulozi do DB a nebo vygeneruje formular s nactenymi daty a 
			//hlaskou o dolpneni udaju, ci chybe.
			formProces();
		} catch (ErrorDatabase e) {
			message = e.getMessage() + "-" + e.getErrorCode();
		}
	}
	
	/**
	 * ziskani parametry z formulare (pokud neni doplni na prazdny retezec)
	 */
	public void fetchParametrs(){
		//nacte session
		HttpSession session = request.getSession(true);
		//nacitani. Prvi v parametrech a pak ze session
		cityName = request.getParameter(CITY_NAME);
		if(cityName == null){
			cityName = (String)session.getAttribute(CITY_NAME);
		    if(cityName == null){
		    	cityName = "";
		    }
		}
		//nacitani. Prvi v parametrech a pak ze session
		mapName = request.getParameter(MAP_NAME);
		if(mapName == null){
		    mapName = (String)session.getAttribute(MAP_NAME);
		    if(mapName == null){
		    	mapName = "";
		    }
		}
		//nacitani. Prvi v parametrech a pak ze session
		shortDesc = request.getParameter(SHORT_DESC);
		if(shortDesc == null){
			shortDesc = (String)session.getAttribute(SHORT_DESC);
		    if(shortDesc == null){
		    	shortDesc = "";
		    }
		}
		//nacitani. Prvi v parametrech a pak ze session
		longDesc = request.getParameter(LONG_DESC);
		if(longDesc == null){
			longDesc = (String)session.getAttribute(LONG_DESC);
		    if(longDesc == null){
		    	longDesc = "";
		    }
		}
        //
        saveAtributsAndParametrs();
	}
	
	/**
	 * Prepise vsecky atributy do session.
	 */
	public void saveAtributsAndParametrs(){
		//nacte session
		HttpSession session = request.getSession(true);
		session.setAttribute(Navigation.SESIION_MESSAGE, message);
		//ulozi parametry
		session.setAttribute(CITY_NAME, cityName);
		session.setAttribute(MAP_NAME, mapName);
		session.setAttribute(SHORT_DESC, shortDesc);
		session.setAttribute(LONG_DESC, longDesc);
	}
	
	public void formProces() throws ErrorDatabase{
		HttpSession session = request.getSession(true);
		//------------zpracovava formular a uklada mesto po zadani akce ulozit mesto-----
        String action = request.getParameter(AddCityUtil.ACTION);
        //podminka: muselo byt zvoleno tlacitko pro ulozani
        if(action != null && action.equals(AddCityUtil.INSERT_CITY_ACTION)){
            //**********jmeno mesta bylo zadano pak:*************
            if(cityName.length() > 0){
                //vytvoreni instance mesta a inicializece hodnot daty z formulare
                City newCity = new City();
                newCity.setName(cityName);
                newCity.setMapyNazov(mapName);
                newCity.setshortDescription(shortDesc);
                newCity.setlongDescription(longDesc);
                //zapsani do databaze
                Table_city tc = Table_city.getInstance();
                String cityID = tc.insertCity(newCity);
                session.setAttribute(CITY_ID, cityID);
                    //informativni hlaska
                    message = "Město bylo úspěšně přidáno";
                    session.setAttribute(NEXT_PAGE, Navigation.JSP_INSERT_CITY_PICTURES);
                
            //***********jmeno mesta nebylo zadano***************
            }else{
                message = "Musíte zadat název města aby mohlo být přidáno";
            }
        }
        //prida do session vyslednou zpravu o tom jak to dopadlo
        makeMessage();
        //prida do session parametry (atributy)
        saveAtributsAndParametrs();
	}
	
	
	//--------------------metody GET----------------
	/*
	public String getCityName(){
		return cityName;
	}
	
	public String getMapName(){
		return mapName;
	}
	
	public String getShortDescription(){
        return shortDesc;
	}
	
	public String getLongDescription(){
        return longDesc;
	}
	*/
	private void makeMessage(){
		HttpSession session = request.getSession(true);
		session.setAttribute(Navigation.SESIION_MESSAGE, message);
	}
	
}
